//
//  EnlargeMapViewController.m
//  Runone
//
//  Created by runone02 on 16/9/13.
//  Copyright © 2016年 runone. All rights reserved.
//

#import "EnlargeMapViewController.h"
#import <MAMapKit/MAMapKit.h>
#import <AMapFoundationKit/AMapFoundationKit.h>
#import "Define.h"
#import "CameraGunDetailViewController.h"
#import "AllLastestVehicleDynamic.h"
#import "ROPointAnnotation.h"
#import "CustomAnnotationView.h"
#import "AppDelegate.h"
#import "OneConst.h"

#define   kScreenH    [UIScreen mainScreen].bounds.size.height
@interface EnlargeMapViewController () <MAMapViewDelegate>

@property (nonatomic, strong) MAMapView *mapView;

@property (nonatomic, strong) NSMutableArray *annoArray;

@end

@implementation EnlargeMapViewController

-(void)setLa:(double)la {
    _la = la;
}
-(void)setLo:(double)lo {
    _lo = lo;
}
-(void)setType:(AllIconType)type {
    _type = type;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    _annoArray = [[NSMutableArray alloc] init];
    [self loadMapview];//加载地图
    [self configUI];
    [self setLatitude:_la Longitude:_lo withIconType:_type];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = NO;
}

- (void) loadMapview {
    [AMapServices sharedServices].apiKey = GETGAODEPAIKEY;
    _mapView = [[MAMapView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds))];
    _mapView.delegate     = self;
    _mapView.showsCompass = NO;
    _mapView.showsScale   = YES ;
    _mapView.scaleOrigin  = CGPointMake(0, kScreenH - 44);
    [self.view addSubview:_mapView];
}

- (void) configUI {
    // 缩小按钮
    UIButton *narrowBtn  = [UIButton buttonWithType:UIButtonTypeCustom];
    [narrowBtn setBackgroundImage:[UIImage imageNamed:@"map_narrow"] forState:UIControlStateNormal];
//    [narrowBtn setBackgroundImage:[UIImage imageNamed:@"suoxiao"] forState:UIControlStateSelected];
    narrowBtn.frame = CGRectMake(CGRectGetWidth(self.view.bounds) - SecondMapBtnWH - 20, 50, SecondMapBtnWH, SecondMapBtnWH);
    [narrowBtn addTarget:self action:@selector(narrow) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:narrowBtn];
    // 路况
    UIButton *TrafficBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [TrafficBtn setBackgroundImage:[UIImage imageNamed:@"traffic_light"] forState:UIControlStateNormal];
    [TrafficBtn setBackgroundImage:[UIImage imageNamed:@"traffic_light_selected"] forState:UIControlStateSelected];
    TrafficBtn.frame  = CGRectMake(CGRectGetWidth(self.view.bounds) - SecondMapBtnWH - 20, 50 + SecondMapBtnWH + ButtunMargin, SecondMapBtnWH, SecondMapBtnWH);
    [TrafficBtn addTarget:self action:@selector(trafficBtn:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:TrafficBtn];
}
#pragma mark - 缩小  显示交通
- (void) narrow {
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (void) trafficBtn:(UIButton *)btn {
    btn.selected = !btn.selected;
    if (btn.selected) {
        _mapView.showTraffic=YES;
    } else {
        _mapView.showTraffic=NO;
    }
}
#pragma mark - privvate

-(void)setLatitude:(CGFloat)la Longitude:(CGFloat)lo withIconType:(AllIconType)type {
    ROPointAnnotation *centerAnnotaion  = [[ROPointAnnotation alloc] init];
    CLLocationCoordinate2D ret          = CLLocationCoordinate2DMake(la, lo);
    centerAnnotaion.coordinate          = ret;
    [_annoArray addObject:centerAnnotaion];
    [_mapView addAnnotations:_annoArray];
    [_mapView showAnnotations:_annoArray animated:NO];
}

- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id <MAAnnotation>)annotation {
    if ([annotation isKindOfClass:[MAPointAnnotation class]])
    {
        static NSString *pointReuseIndentifier = @"pointReuseIndentifier";
        CustomAnnotationView *annotationView   = (CustomAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:pointReuseIndentifier];
        if (annotationView == nil) {
            annotationView = [[CustomAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:pointReuseIndentifier];
        }
        annotationView.selected              = NO;
        switch (_type) {
            case 0:
                //事故事件
                annotationView.image = [UIImage imageNamed:@"accident_icon"];
                break;
            case 1:
                //施工事件
                annotationView.image = [UIImage imageNamed:@"main_line_construction"];
                break;
            case 2:
                //危险品车辆
                annotationView.image = [UIImage imageNamed:@"dynamic_vehilce"];
                break;
            case 3:
                //营运客车
                annotationView.image = [UIImage imageNamed:@"commercial _vehicle"];
                break;
            case 4:
                //救援
                annotationView.image = [UIImage imageNamed:@"save_move"];
                break;
            case 5:
                //救援0
                 annotationView.image = [UIImage imageNamed:@"save_stop"];
                break;
            case 6:
                //行政
                annotationView.image = [UIImage imageNamed:@"administrative_move"];
                break;
            case 7:
                //行政0
                annotationView.image = [UIImage imageNamed:@"administrative_stop"];
                break;
            case 8:
                //路政
                annotationView.image = [UIImage imageNamed:@"road_admin_move"];
                break;
            case 9:
                //路政0
                annotationView.image = [UIImage imageNamed:@"road_admin_stop"];
                break;
            case 10:
                //摄像枪
                annotationView.image = [UIImage imageNamed:@"map_camera_gun"];
                break;
            case 11:
                //收费站
                 annotationView.image = [UIImage imageNamed:@"station_icon"];
                break;
            case 12:
                //扎到
                annotationView.image = [UIImage imageNamed:@"ramp_construction"];
                break;
            case 13: //养护
//                annotationView.image = [UIImage imageNamed::@"conservation_move"];
                annotationView.image = [UIImage imageNamed:@"conservation_move"];
                break;
            
            case 14:
                annotationView.image = [UIImage imageNamed:@"conservation_stop"];
                break;
            default:
                break;
        }
        annotationView.centerOffset = CGPointMake(0, -annotationView.image.size.height / 2);
        return annotationView;
    }
    return nil;
}

- (void)mapView:(MAMapView *)mapView didSelectAnnotationView:(MAAnnotationView *)view {
    CustomAnnotationView *customView = (CustomAnnotationView *)view;
    customView.selected              =  NO;
}
-(void)mapView:(MAMapView *)mapView regionDidChangeAnimated:(BOOL)animated {
    NSLog(@"%f--%f--%f--%f--%f",mapView.region.span.latitudeDelta,mapView.region.span.longitudeDelta,mapView.zoomLevel,mapView.region.center.latitude,mapView.region.center.longitude);
}
@end
